package com.gues.ddc_app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gues.ddc_app.entity.*;
import com.gues.ddc_app.service.IOrdersService;
import com.gues.ddc_app.service.impl.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.spring5.context.SpringContextUtils;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huHang
 * @since 2023-02-12
 */
@RestController
@CrossOrigin//跨域
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;
    @RequestMapping("/addOrder")
    public Object addOrder(Orders order){
        System.out.println(order);
        order.setTime(new Date());
        Integer qu=(int)((Math.random()*9+1)*1000);
        order.setQucanhao(qu);
        if(ordersService.save(order)){
            return order.getId();
        }
        return null;
    }
    @RequestMapping("/queryAll")
    public ResultData<Orders> queryAll(Integer page, Integer limit, @RequestParam(defaultValue = "") String keyword){
        Page<Orders> menuPage = new Page<>(page,limit);
        ordersService.page(menuPage);
        int a=(int)menuPage.getTotal();
        ResultData<Orders> userResultData = new ResultData<>((Integer)a,menuPage.getRecords());
        return userResultData;
    }
    @RequestMapping("/queryAll1")
    public Object queryAll1(Integer state, HttpSession session){
        User user =(User) session.getAttribute("user");
        if(user!=null){
            QueryWrapper<Orders> wrapper=new QueryWrapper<>();
            if(state==0){
                wrapper.eq("o.user_id",user.getUserId());
                wrapper.eq("state","待支付");
                wrapper.or();
                wrapper.eq("o.user_id",user.getUserId());
                wrapper.eq("state","制作中");
            }else {
                wrapper.eq("o.user_id",user.getUserId());
                wrapper.eq("state","已完成");
                wrapper.or();
                wrapper.eq("o.user_id",user.getUserId());
                wrapper.eq("state","已取消");
            }
            return ordersService.getList(wrapper);
        }
        else {
            Shop shop=(Shop) session.getAttribute("shop");
            QueryWrapper<Orders> wrapper=new QueryWrapper<>();
            if(state==0){
                //wrapper.eq("o.shop_id",shop.getShopId());
                //wrapper.eq("state","待支付");
                //wrapper.or();
                wrapper.eq("o.shop_id",shop.getShopId());
                wrapper.eq("state","制作中");
            }else {
                wrapper.eq("o.shop_id",shop.getShopId());
                wrapper.eq("state","已完成");
            }
            return ordersService.getList(wrapper);
        }
    }
    @RequestMapping("/queryById")
    public Object queryById(Integer id){
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        return ordersService.getList(wrapper);
    }
    @RequestMapping("/upOrderState")
    public Object upOrderState(Integer id,Integer state){
        Orders orders=new Orders();
        orders.setId(id);
        switch (state){
            case 0:
                orders.setState("待支付");
                break;
            case 1:
                orders.setState("制作中");
                break;
            case 2:
                orders.setState("已完成");
                break;
            case 3:
                orders.setState("已取消");
                break;
        }
        ordersService.updateById(orders);
        return ordersService.getById(id);
    }
}
